package com.prj.ufdm.core.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletRequest;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/** 
* @Description: Request 工具
* @date 2018年3月20日 
* @author 胡义振
*/
public class UfdmRequestUtil {
	
	
	/**
	 * 描述: 通过 request 对象获取所有参数
	 * @auther 胡义振
	 * @date 2018-03-20 
	 * @param request
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static Map<String, String> getHeaderValues(HttpServletRequest request) {
		Map<String,String> tempMap = new HashMap<String,String>();
		try
		{
			Enumeration enuParam = request.getHeaderNames();  
			while(enuParam.hasMoreElements()){  
				String headName = (String) enuParam.nextElement();
				tempMap.put(headName, request.getHeader(headName));
			}  
		}
		catch(Exception er){
			
		}
		return tempMap;
	}
	
	
	/**
	 * 描述: 通过 request 对象获取所有参数
	 * @auther 胡义振
	 * @date 2018-03-20 
	 * @param request
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static Map<String, String> getAllParam(HttpServletRequest request) {
		Map<String,String> tempMap = new HashMap<String,String>();
		try
		{
			Enumeration enuParam = request.getParameterNames();  
			while(enuParam.hasMoreElements()){  
				String paramName = (String) enuParam.nextElement();
				String [] arrParamValue = request.getParameterValues(paramName);
				String paramValue = UfdmStringUtil.arrayToStr(arrParamValue, ",");
				// 过滤掉空值
				if(paramValue!=null && paramValue.length()>0){
				    tempMap.put(paramName, paramValue);
				}
			}  
		}
		catch(Exception er){
			
		}
		return tempMap;
	}
	
	
	/**
	 * 描述: 通过 request 对象获取IP地址
	 * @auther 胡义振
	 * @date 2018-03-20 
	 * @param request
	 * @return
	 */
	public static String getIpAddress(HttpServletRequest request)
	{
		if(request!=null)
		{
			String ipAddress = request.getHeader("X-Forwarded-For");
		    if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
		    	ipAddress = request.getHeader("Proxy-Client-IP");
		    }
		    if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
		    	ipAddress = request.getHeader("WL-Proxy-Client-IP");
		    }
		    if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
		    	ipAddress = request.getHeader("X-Real-IP");
		    }
		    if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
		    	ipAddress = request.getRemoteAddr();
		    }
		    return ipAddress;
		}
		else
		{
			return "";
		}
	}

	/**
	 * 描述: 从 request 中获取 cookie 值
	 * @auther 胡义振
	 * @date 2018-03-20 
	 * @param request
	 * @param cookieName
	 * @return
	 */
	public static Cookie getCookieValue(HttpServletRequest request,String cookieName) {
		Cookie[] cookie = request.getCookies();
		for (int i = 0; cookie != null && i < cookie.length; i++) {
			if (cookie[i].getName().equals(cookieName))
				return cookie[i];
		}
		return null;
	}
	

	/**
	 * 描述: 从 request 中获取流信息
	 * @auther 胡义振
	 * @date 2014-10-13
	 * @param request
	 * @param encode
	 * @return
	 */
	public static String getStringFromStream(ServletRequest request,String encode) throws Exception {
		StringBuffer result = new StringBuffer();
		InputStream in = null;
		try
		{
			in = request.getInputStream();
			if(in!=null){
				InputStreamReader inStreamRead = new InputStreamReader(in,encode);
				BufferedReader buffRead = new BufferedReader(inStreamRead);
				String line;
				while((line = buffRead.readLine()) != null)
				{
					result.append(line);
				}
			}
		}
		catch(Exception er){
			er.printStackTrace();
		}
		finally {
			if(in!=null) {
				in.close();
			}
		}
		return result.toString();
	}
	
	
	/**
	 * 描述: 在 response 中增加 csrf 头信息
	 * @auther 胡义振
	 * @date 2018-03-20 
	 * @param request
	 * @param encode
	 * @return
	 */
	public static void addCsrfHeader(HttpServletResponse response) {
		response.setHeader("content-type", "text/html;charset=UTF-8");
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
		response.setHeader("Access-Control-Max-Age", "3600");
		response.addHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, UFDM-AuthToken");
	}
	
	public static void main(String args [] ){
		try
		{
			String reqUrl = "http://192.168.70.178:5204/fs/fileRelay/fileRelayMethod";
			
			Map<String,String> reqHeadMap = new HashMap<String, String>();
			reqHeadMap.put("ispToken", "f310b29caa5bf746bcf9e79d6207597a");
			
			Map<String,String> reqParamMap = new HashMap<String, String>();
			
			Map<String, File> reqFileMap = new HashMap<String, File>();
			File file1 = new File("D:\\test.png");
			reqFileMap.put("file1", file1);
			
			String result = UfdmRequestHttpUtil.postFile(reqUrl, reqHeadMap, reqParamMap, reqFileMap, "utf-8");
			
			System.out.println(result);
			
			
		}
		catch(Exception er){
			er.printStackTrace();
		}
	}
	
}
